package com.dao;

import com.bean.讲授;
import com.bean.讲授;
import com.jdbc.DataSourceTool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class 讲授Dao {
    //增
    public boolean insertTeach(讲授 teach) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "insert into 讲授(教师编号,课程代号,任课时间)" +
                "values (?,?,?)";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,teach.get教师编号());
            ps.setString(2,teach.get课程代号());
            ps.setString(3,teach.get任课时间());

            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }
    //删
    public boolean deleteTeach(String 课程代号) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "delete from 讲授 where 课程代号 = ?";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,课程代号);

            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }

    //改
    public boolean updateTeach(讲授 teach) throws SQLException {
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "update 讲授 set 教师编号=?,任课时间=? where 课程代号=?; ";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //给sql赋值
            ps.setString(1,teach.get教师编号());
            ps.setString(2,teach.get任课时间());
            ps.setString(3, teach.get课程代号());

            //执行sql
            int resul = ps.executeUpdate();
            //
            if(resul>0){
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return false;
    }
    //查 -- 返回结果集
    public List<讲授> queryTeach() throws SQLException {
        //创建集合用于保存数据
        List<讲授> list = new ArrayList<>();
        //通过DataSourceTool获取数据库链接对象
        Connection connection = DataSourceTool.getConnection();
        //定义sql
        String sql = "select * from 讲授;";
        //创建sql对象
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            //执行sql
            ResultSet rs = ps.executeQuery();
            //遍历结果集
            while(rs.next()){
                //创建一个讲授类型的数据用于获取数据库中相同类型的数据
                讲授 teach=new 讲授();
                teach.set教师编号(rs.getString(1));
                teach.set课程代号(rs.getString(2));
                teach.set任课时间(rs.getString(3));

                //把查询结果放入集合中
                list.add(teach);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭链接
            DataSourceTool.closeConnection(connection);
        }
        return list;
    }

}
